import { existsSync, readFileSync, statSync } from 'fs';
import { join } from 'path';
import { logger } from '../utils/logger';
import { CardLoader } from './CardLoader';
import { DeptLoader } from './DeptLoader';
import { PoolLoader } from './PoolLoader';
import { ScriptLoader } from './ScriptLoader';

export interface IModJSON {
	title: string;
	desc: string;
	author: string;
	version: string;
}

export class Mod {
	constructor(dir: string) {
		if (!statSync(dir).isDirectory()) return;
		logger.info(`LoadMod: ${dir}`);
		const json = this.loadJSON(join(dir, 'mod.json'));
		if (!json) return;
		// 加载脚本
		new ScriptLoader(json).loadScript(join(dir, 'main.js'));
		// 加载卡牌
		new CardLoader(json).loadCards(join(dir, 'cards'));
		// 加载部门
		new DeptLoader(json).loadDepts(join(dir, 'depts'));
		// 加载卡池
		new PoolLoader(json).loadPools(join(dir, 'pools'));
	}

	loadJSON(path: string) {
		if (!existsSync(path)) return;
		logger.info(`Loading from: ${path}`);
		return JSON.parse(readFileSync(path, 'utf-8'));
	}
}
